package cn.time1024.time_app.ui.home

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.time1024.time_app.R

data class HomeMenuItem(
    val id: String,
    val title: String,
    val iconRes: Int,
    val iconTint: String,
    val bgColor: String,
    val onClick: () -> Unit
)

class HomeMenuAdapter(private val items: List<HomeMenuItem>) : RecyclerView.Adapter<HomeMenuAdapter.VH>() {
    class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iconContainer: LinearLayout = itemView.findViewById(R.id.iconContainer)
        val imgIcon: ImageView = itemView.findViewById(R.id.imgIcon)
        val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.home_menu_item, parent, false)
        return VH(v)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = items[position]
        holder.iconContainer.setBackgroundColor(Color.parseColor(item.bgColor))
        holder.imgIcon.setImageResource(item.iconRes)
        holder.imgIcon.setColorFilter(Color.parseColor(item.iconTint))
        holder.tvTitle.text = item.title
        holder.itemView.setOnClickListener { item.onClick() }
    }
}

